/**
 * 〈〉
 *
 * @author 王青杰
 * @create 2018-08-0810:20
 * @since 1.0.0
 */
package org.wlgzs.website.wlgzsoffice.service.impl;

import java.util.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wlgzs.website.wlgzsoffice.dao.GradeDao;
import org.wlgzs.website.wlgzsoffice.entity.TbGrade;
import org.wlgzs.website.wlgzsoffice.enums.ResultEnum;
import org.wlgzs.website.wlgzsoffice.service.GradeService;
import org.wlgzs.website.wlgzsoffice.util.ResultMsg;

import javax.annotation.Resource;

@Service
public class GradeServiceImpl implements GradeService{
    @Resource
    GradeDao gradeDao;

    @Override
    @Transactional
    public ResultMsg insert(TbGrade tbGrade) {
        if(tbGrade!=null){
            gradeDao.insert(tbGrade);
            return new ResultMsg(ResultEnum.SAVE,tbGrade);
        }
        return new ResultMsg(ResultEnum.UNSAVE,tbGrade);
    }

    @Transactional
    @Override
    public ResultMsg delete(int gradeId) {
        if(gradeDao.selectByPrimaryKey(gradeId)!=null){
            gradeDao.deleteByPrimaryKey(gradeId);
            PageHelper.startPage(0,10);
            List<TbGrade> gradeList = gradeDao.checkAll();
            PageInfo pageInfo = new PageInfo(gradeList);
            return new ResultMsg(ResultEnum.DELETE,pageInfo);
        }
        PageHelper.startPage(0,10);
        List<TbGrade> gradeList = gradeDao.checkAll();
        PageInfo pageInfo = new PageInfo(gradeList);
        return new ResultMsg(ResultEnum.UNDELETE,pageInfo);

    }

    @Transactional
    @Override
    public ResultMsg update(TbGrade tbGrade) {
        if(gradeDao.selectByPrimaryKey(tbGrade.getGradeId())!=null){
            gradeDao.updateByPrimaryKeySelective(tbGrade);
            return new ResultMsg(ResultEnum.UPDATE,tbGrade);
        }
        return new ResultMsg(ResultEnum.UNUPDATE,tbGrade);
    }

    @Transactional
    @Override
    public ResultMsg check(int pageNum, TbGrade grade) {
        if(grade!=null) {
            if (grade.getAssessId() == null) {
                grade.setAssessId(0);
            }
            if (grade.getJudgementId() == null) {
                grade.setJudgementId(0);
            }
            if (grade.getApplicationId() == null) {
                grade.setApplicationId(0);
            }
        }
        System.out.println(grade.getAssessId());
        System.out.println(grade.getJudgementId() );
        System.out.println(grade.getApplicationId());

        PageHelper.startPage(pageNum,10);
        List<TbGrade> gradeList = gradeDao.check(grade.getApplicationId(),grade.getJudgementId(),grade.getAssessId());
        //List<TbGrade> gradeLis t = gradeDao.checkAll();
        PageInfo pageInfo = new PageInfo(gradeList);
        return new ResultMsg(ResultEnum.QUIRY,pageInfo);
        }
}
